<template>
    <div class="supplyInfoMaincon">
        <div class="infolist">
          <div class="headerSelection">
            <split-vertical title="供应商信息"></split-vertical>
            <v-form :formProps="searchForm" ref="form"
                    v-on:handleReset="handleReset"
                    v-on:coverFormData="coverFormData"></v-form>
          </div>
            <div class="stand">
              <div style="line-height:50px;margin-left:10px;">
                <v-button :operationBtnArray="operationBtnArray" v-on:handleSearchTopBtn="handleSearchTopBtn"></v-button>
              </div>
              <div style="margin:0 10px;">
                <v-table :tableParams="tableParams" v-on:handleOperation="handleOperation"></v-table>
              </div>
            </div>
            <div class="block">
                <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[10]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageCount">
                </el-pagination>
            </div>
           <!-- 弹出框组件 -->
                <edit-info v-if="show" @closeDialog='closeDialog' :addinfo='addinfo' ></edit-info>
                <add-info v-if='showtip' @closeDialog='closeDialog'></add-info>
        </div>
    </div>
</template>
<script>

import VTable from "components/common/PageTable/VTable"
import addInfo from 'components/base/SupplierInfo/AddInfo.vue'
import editInfo from 'components/base/SupplierInfo/EditInfo.vue'
import SplitVertical from "components/common/SplitVertical"
import VForm from "components/common/VForm"
import VButton from "components/common/VButton"
let changeMethods={
    '1':'直采直销',
    '2':'一件代发'
}

export default {
    inject:['reload'],
    components:{
      addInfo,editInfo,VTable,SplitVertical,VForm,VButton
    },
    data(){
        return{
             //供应商列表
            show:false,
            showtip:false,
            pageCount:0,
            pageIndex:1,
            pageSize:10,
            //传的对象
            addinfo:{},
            supplyName:'',
            supplyCode:'',
            searchForm: {
              col: 3,
              labelWidth: '100px',
              submitBtn: {
                text: '搜索'
              },
              formItemArray: [{
                label: '供应商名称',
                prop: 'supplyName',
                placeholder: "请输入供应商名称",
                value: ''
              }, {
                label: '供应商编码',
                prop: 'supplyCode',
                placeholder: "请输入供应商编码",
                value: ''
              }]
            },
            operationBtnArray: [{
              text: '刷新',
              icon:'el-icon-refresh',
              show: true,
              type: 'primary',
              methodName: 'refresh',
            }, {
              text: '新增',
              show: true,
              icon:'el-icon-plus',
              type: 'success',
              methodName: 'add',
            }],
            tableParams:{
                dataArray:[],
                operationColumnWidth:100,
                showMultiSelection:false,
                columnArray:[
                    {label: '供应商名称', prop: 'supplyName',sortable: false,width:'180'},
                    {label: '供应商编码', prop: 'supplyCode',width:'110',sortable: false},
                    {label: '授权品牌', prop: 'accreditBrand',  sortable: false},
                    {label: '公司地址', prop: 'supplyAddress',sortable: false},
                    {label: '公司三证', prop: 'supplyCertificate',width:'80',sortable: false},
                    {label: '合作方式', prop: 'cooperationType',sortable: false,width:'80'},
                    {label: '开户行账户', prop: 'supplyAccount',sortable: false,width:'180'},
                    {label: '开户行名称', prop: 'supplyAccountName',sortable: false,width:'150'},
                    {label: '联系人', prop: 'supplyContact',sortable: false,width:'80'},
                    {label: '联系人电话', prop: 'supplyMobile',sortable: false,width:'110'},
                    {label: '联系人职位', prop: 'supplyContactPosition',sortable: false,width:'95'}
                ],
                rowHandle:{
                    view:{
                        show:false
                    }
                }
            },
            searchCondition:{}
        }
    },
    created(){
      this.init()
    },
    methods:{
        init(){
           this.supplyList()
        },
        handleSearchTopBtn(methodName) {
          if (methodName === "refresh") {
            this.refresh();
          } else {
            this.create();
          }
        },
        handleReset(formName) {
          this.$refs.form.resetForm(formName);
        },
        coverFormData(data) {
          this.searchCondition = data;
          this.query()
        },
        handleOperation(rowIndex, row, methodName) {
            this.id=row.id
            if(methodName == "update"){
               this.showTip=true
               this.modifySupplyContent()
            }else if(methodName == "delete"){
                this.delSupplyContent()
            }
        },
        //初始化供应商接口
        supplyList(){
            let parmas={
                pageIndex:1,
                pageSize:10,
            }
            let success=data=>{
                if(data.code=='OK'){
                    this.pageCount = data.pageCount
                    this.tableParams.dataArray= data.data
                    data.data.forEach(element => {
                        element.cooperationType=changeMethods[element.cooperationType]
                    });
                }else{
                    this.$message.error(data.msg)
                }
            }
            this.api.supplyList(parmas,success)
        },
        //刷新列表
        refresh(){
            this.supplyList()
        },
        //查询
        query(){
          let param = this.searchCondition;
          if (!param.pageIndex || !param.pageSize) {
            param['pageIndex'] = this.pageIndex;
            param['pageSize'] = this.pageSize;
          }
          let success = data => {
            if (data.code === 'OK') {
              this.pageCount = data.pageCount;
              this.tableParams.dataArray=data.data
              data.data.forEach(element => {
                element.cooperationType=changeMethods[element.cooperationType]
              });
            } else {
              this.$message.error(data.msg);
            }
          };
          this.api.supplyList(param,success)
         },
        //弹框
        create(){
            this.showtip=true;
        },
        //编辑弹窗
        modifySupplyContent(){
            this.show=true;
            let parmas={
                id:this.id
            }
            let success=data=>{
                if(data.code='OK'){
                    this.addinfo=data.data
                }else{
                    this.$message.error(data.msg)
                }
            }
           this.api.supplyGetOne(parmas,success)
        },
        //关闭弹窗
        closeDialog(){
            this.showtip=false;
            this.show=false;
        },
        delSupplyContent(){
            this.$confirm('是否删除该消息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
            }).then(() => {
                //删除供应商列表信息
                let parmas={
                    id:this.id
                }
                let success=data=>{
                    if(data.code=='OK'){
                        this.reload()
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }else{
                        this.$messsage.error(data.msg)
                    }
                }
                this.api.supplyDelete(parmas,success)
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //当前页
        handleCurrentChange(val) {
          this.searchCondition['pageIndex'] = val;
          this.query()
        }
    },

}
</script>
<style lang="scss" scoped>
.supplyInfoMaincon{
    width: 100%;
    height:auto;
   .infolist{
     .headerSelection{
       background:#fff;
       height:140px;
       padding: 0 10px;
       border-left: 2px solid #e8e8e8;
       border-right: 2px solid #e8e8e8;
       border-bottom: 2px solid #e8e8e8;
     }
     .stand{
       background:#fff;
     }
     .block{
       padding:10px 0;
       text-align: right;
     }
   }
}
</style>
